import request from '@/utils/axios'
import type { SkuType, SpuType } from '@/api/mall/product/spu'

export interface SecKillActivityType {
  id: number
  spuId?: number
  name?: string
  status?: number
  remark?: string
  startTime?: Date
  endTime?: Date
  sort?: number
  configIds?: string
  orderCount?: number
  userCount?: number
  totalPrice?: number
  totalLimitCount?: number
  singleLimitCount?: number
  stock?: number
  totalStock?: number
  products?: SecKillProductType[]
}

// 秒杀活动所需属性
export interface SecKillProductType {
  skuId: number
  secKillPrice: number
  stock: number
}

// 扩展 Sku 配置
export type SkuTypeExtension = SkuType & {
  productConfig: SecKillProductType
}

export interface SpuExtension extends SpuType {
  skuList: SkuTypeExtension[] // 重写类型
}

// 秒杀时段配置
export interface SecKillConfigType {
  id: number // 编号
  name: string // 秒杀时段名称
  startTime: string // 开始时间点
  endTime: string // 结束时间点
  sliderPicUrls: string[] // 秒杀轮播图
  status: number // 活动状态
}

// 秒杀 API
export const SecKillApi = {
  // 秒杀活动接口
  Activity: {
    // 新增秒杀活动
    create: async (data: SecKillActivityType) => {
      return await request.post({ url: '/promotion/secKill-activity/create', data, is_mock: true })
    },
    // 修改秒杀活动
    update: async (data: SecKillActivityType) => {
      return await request.put({ url: '/promotion/secKill-activity/update', data, is_mock: true })
    },
    // 删除秒杀活动
    delete: async (id: number) => {
      return await request.delete({ url: '/promotion/secKill-activity/delete?id=' + id, is_mock: true })
    },
    // 关闭秒杀活动
    close: async (id: number) => {
      return await request.put({ url: '/promotion/secKill-activity/close?id=' + id, is_mock: true })
    },
    // 查询秒杀活动详情
    detail: async (id: number) => {
      return await request.get({ url: '/promotion/secKill-activity/get?id=' + id, is_mock: true })
    },
    // 查询秒杀活动列表
    page: async params => {
      return await request.get({ url: '/promotion/secKill-activity/page', params, is_mock: true })
    }
  },
  // 秒杀时段相关接口
  Config: {
    // 新增秒杀时段
    create: async (data: SecKillConfigType) => {
      return await request.post({ url: `/promotion/secKill-config/create`, data, is_mock: true })
    },
    // 修改秒杀时段
    update: async (data: SecKillConfigType) => {
      return await request.put({ url: `/promotion/secKill-config/update`, data, is_mock: true })
    },
    // 修改时段配置状态
    updateStatus: async (id: number, status: number) => {
      const data = {
        id,
        status
      }
      return request.put({ url: '/promotion/secKill-config/update-status', data, is_mock: true })
    },
    // 删除秒杀时段
    delete: async (id: number) => {
      return await request.delete({ url: `/promotion/secKill-config/delete?id=` + id, is_mock: true })
    },
    // 查询秒杀时段详情
    detail: async (id: number) => {
      return await request.get({ url: `/promotion/secKill-config/get?id=` + id, is_mock: true })
    },
    // 查询秒杀时段分页
    page: async (params: any) => {
      return await request.get({ url: `/promotion/secKill-config/page`, params, is_mock: true })
    },
    // 查询秒杀时段列表
    list: async () => {
      return await request.get({ url: `/promotion/secKill-config/list`, is_mock: true })
    }
  }
}
